package com.common;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import com.common.utils.SpringContextHolder;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil {
	private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
    public static Properties portalProp = SpringContextHolder.getBean("portalProperties",Properties.class);
	public static void main(String[] args) {
		String ss = "<html><body>";
		ss+= "<div>lalal</div>";
		ss+= "</body></html>";
		//相对路径
		FileUtil.writeFile("src/main/resources/templates/aa.html", ss);
		HttpServletRequest request = null;
		request.getServletContext().getContextPath();
		FileUtil.writeFile("src/main/resources/templates/aa.html", ss);
	}
	public static void writeFile(){
		String fileContent = "<html><body>";
		fileContent+= "<div>lalal</div>";
		fileContent+= "</body></html>";
        FileUtil.writeFile2(fileContent,"test/","lalaa.html");
        FileUtil.writeClassFile2(fileContent,"test/","lalaa.html");
	}

	public static void writeFile2(String fileContent,String saveFolderName,String saveFileName){
		FileUtil.writeFile(portalProp.getProperty("projectPath")+"/src/main/resources/webform/"+saveFolderName+saveFileName, fileContent);
	}
    public static void writeClassFile2(String fileContent,String saveFolderName,String saveFileName){
        FileUtil.writeFile(portalProp.getProperty("projectPath")+"/target/classes/webform/"+saveFolderName+saveFileName, fileContent);
    }
	
    /**
     * 创建目录
     */
    public static File createDir(String dirPath) {
        File dir;
        try {
            dir = new File(dirPath);
            if (!dir.exists()) {
                FileUtils.forceMkdir(dir);
            }
        } catch (Exception e) {
            logger.error("创建目录出错！", e);
            throw new RuntimeException(e);
        }
        return dir;
    }

    /**
     * 创建文件
     */
    public static File createFile(String filePath) {
        File file;
        try {
            file = new File(filePath);
            File parentDir = file.getParentFile();
            if (!parentDir.exists()) {
                FileUtils.forceMkdir(parentDir);
            }
        } catch (Exception e) {
            logger.error("创建文件出错！", e);
            throw new RuntimeException(e);
        }
        return file;
    }
    /**
     * 删除文件
     */
    public static void deleteFile(String filePath) {
        try {
            File file = new File(filePath);
            if (file.exists() && file.isFile()) {
                FileUtils.forceDelete(file);
            }
        } catch (Exception e) {
            logger.error("删除文件出错！", e);
            throw new RuntimeException(e);
        }
    }
	/**
     * 将字符串写入文件
     */
    public static void writeFile(String filePath, String fileContent) {
        OutputStream os = null;
        Writer w = null;
        try {
            FileUtil.createFile(filePath);
            os = new BufferedOutputStream(new FileOutputStream(filePath));
            w = new OutputStreamWriter(os, "UTF-8");
            w.write(fileContent);
            w.flush();
        } catch (Exception e) {
            logger.error("写入文件出错！", e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (w != null) {
                    w.close();
                }
            } catch (Exception e) {
                logger.error("释放资源出错！", e);
            }
        }
    }
    /**
     * 判断文件是否存在
     */
    public static boolean checkFileExists(String filePath) {
        return new File(filePath).exists();
    }
}
